$(function() {
	admin.couponBase.init();
});
Namespace('admin.couponBase', {
	init : function() {
		easyui.util.initDatagrid('#datagrid', {
			onDblClickRow : function() {
			}
		});
	},
	query : function() {
		if (!$('#query-form').form('validate'))
			return;
		var data = $('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load', data);
	},
	detail:function(id) {
		if (id != null) {
			top.addTab(id,'详情', contextPath+'/coupon/base/detail?id=' + id);
		}
	},
	up: function (id) {
		var tip = "确定开始发行吗";
		$.messager.confirm('信息提示', tip, function (success) {
			if (!success) {
				return;
			}
            var url = contextPath + '/coupon/base/up';
			$('#datagrid').datagrid('loading');
			$.post(url, {'id': id}, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('操作成功');
					var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
					console.log(pageopt.pageNumber);
                    admin.couponBase.query();
				} else {
					top.showInfo(result.msg);
				}
			});

		});
	},
	down: function (id) {
		var tip = "确定停止发行吗";
		$.messager.confirm('信息提示', tip, function (success) {
			if (!success) {
				return;
			}
            var url = contextPath + '/coupon/base/down';
			$('#datagrid').datagrid('loading');
			$.post(url, {'id': id}, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('操作成功');
					var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
					console.log(pageopt.pageNumber);
                    admin.couponBase.query();
				} else {
					top.showInfo(result.msg);
				}
			});

		});
	},
	qBuyUp: function (id) {
		var tip = "确定可以圈币兑换吗";
		$.messager.confirm('信息提示', tip, function (success) {
			if (!success) {
				return;
			}
            var url = contextPath + '/coupon/base/qBuyUp';
			$('#datagrid').datagrid('loading');
			$.post(url, {'id': id}, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('操作成功');
					var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
					console.log(pageopt.pageNumber);
                    admin.couponBase.query();
				} else {
					top.showInfo(result.msg);
				}
			});

		});
	},
	qBuyDown: function (id) {
		var tip = "确定停止圈币兑换吗";
		$.messager.confirm('信息提示', tip, function (success) {
			if (!success) {
				return;
			}
            var url = contextPath + '/coupon/base/qBuyDown';
			$('#datagrid').datagrid('loading');
			$.post(url, {'id': id}, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('操作成功');
					var pageopt = $('#datagrid').datagrid('getPager').data("pagination").options;
					console.log(pageopt.pageNumber);
                    admin.couponBase.query();
				} else {
					top.showInfo(result.msg);
				}
			});

		});
	},
	toAdd:function() {
		top.addTab("1",'添加', contextPath+'/coupon/base/toAdd');
	},
	toUpdate:function(id) {
		if (id != null) {
			top.addTab(id,'编辑', contextPath+'/coupon/base/toUpdate?id=' + id);
		}
	},
	add: function () {
		 	if(!$('#data-form').form('validate')) return;
	        var data=$('#data-form').form('jsonObject');
            var url = contextPath + '/coupon/base/create';
			$.post(url, data, function (result) {
				$('#datagrid').datagrid('loaded');
				if (result.code == 1) {
					top.showInfo('创建成功');
					top.addTab("",'优惠卷列表', contextPath+'/coupon/base/list');
				} else {
					top.showInfo(result.msg);
				}
			});

	},
	netMsgChange:function(id,maxCount) {
		var inputLength = $('#'+id).val().length;
		if (inputLength == 0) {
			$('#'+id+'_txt').html(maxCount+'个字以内，还可以输入' + maxCount + '个字');
			saveFlag = false;
		} else if (inputLength > 0 && inputLength <= maxCount) {
			$('#'+id+'_txt').html(maxCount+'个字以内，还可以输入' + (maxCount - inputLength) + '个字');
			saveFlag = false;
		} else if (inputLength > maxCount) {
			$('#'+id+'_txt').html(maxCount+'个字以内，已经输入超过<strong class="cred">' + (inputLength - maxCount) + '</strong>个字');
			saveFlag = true;
		}
	},
	toUser:function(baseId) {
		top.addTab(baseId,'优惠券用户', contextPath+'/coupon/user/list/'+baseId);
	},
	queryUser : function() {
		if (!$('#query-form').form('validate'))
			return;
		var data = $('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load', data);
	},
	chooseUser:function() {
		getDialog('query-dialog').dialog({
			href:contextPath+'/user/base/choose',
			title: '点击选择用户',
		    width: 900,
		    height: 500,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.couponBase.addUser();
					$('#query-dialog').dialog('close');
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#query-dialog').dialog('close');
				}
			}]
		});
	},
	addUser:function() {
		var rows = $('#datagrid2').datagrid('getSelections');
		if(rows.length==0){  
			alert("请至少选择一行数据!");  
			return false;  
		}
		var userIds = '';
		for(var i=0; i < rows.length; i++){
			if (userIds == '') {
				userIds = rows[i].id;
			} else {
				userIds += ',' + rows[i].id;
			}
		}
		var couponId = $('#couponId').val();
		var pushMsg = $('#pushMsg').val();
		var data = {couponId:couponId,userIds:userIds,pushMsg:pushMsg};
		$.post(contextPath+'/coupon/user/addUser',data,function(result) {
			$('#datagrid').datagrid('reload');
			if (result.code == 1) {
				top.showInfo('保存成功');
				$('#query-dialog').dialog('close');
			} else {
				top.showInfo(result.msg);
			}
		});
	},
	exportUserPhone2Excel : function() {//导出用户的电话到excel
		var url = contextPath + '/coupon/user/exportUserPhone2Excel';
		var couponId = $('#couponId').val();
		var userIdentity = $('#userIdentity').val();
		var status = $('#status').val();
		
		var paramstr ="";
		if(couponId!='' && couponId!=null){
			paramstr+="&couponId="+couponId;
		}
		if(userIdentity!='' && userIdentity!=null){
			paramstr+="&userIdentity="+userIdentity;
		}
		if(status!='' && status!=null){
			paramstr+="&status="+status;
		}
		if(paramstr!=''){
			url += "?"+paramstr;
		}
		window.open(url,"");
	}
});